package com.my.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author coderMy
 * @Date 2021/3/30
 */
@RestController
public class RefreshController {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${server.port}")
    private Integer port;

    /**
     * 修改gitee配置文件后，gitee发送请求到这里
     * 我们调用配置中心内部接口，将刷新请求推送到MQ中，服务中集成配置中心客户端，监听到刷新请求
     * 就会从配置中心重新拉取配置文件，进行更新
     * @return
     */
    @RequestMapping("/refreshConfig")
    public ResponseEntity refreshConfig(){

        String url = "http://localhost" + port + "/actuator/bus-refresh";
        HttpHeaders headers = new HttpHeaders();
        headers.add("content-type","application/json;charset=utf-8");
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, entity, String.class);
        if (stringResponseEntity.getStatusCode().equals(HttpStatus.NO_CONTENT)){
            return ResponseEntity.ok("刷新成功");
        }
        return ResponseEntity.ok("刷新失败");
    }

}
